package com.maibow.service.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maibow.dao.DaoSupport;
import com.maibow.entity.admin.Module;
import com.maibow.entity.admin.OperPrivilege;
import com.maibow.entity.admin.Role;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.service.system
 * 文件名：RoleService.java
 * 作者: 吴比(Wubi)
 * 时间：2015年3月27日-下午7:37:46
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Service("newroleService")
public class NewRoleService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    /**
     * 查询角色列表
     * 
     * @return
     * @throws Exception
     */
    public List<Role> listAllRoles() throws Exception {
        return (List<Role>) dao.findForList("NewRoleMapper.listAllRoles", null);
    }

    /**
     * 查询角色列表，包括超级管理员
     * 
     * @return
     * @throws Exception
     */
    public List<Role> listAllRolesHasSuper() throws Exception {
        return (List<Role>) dao.findForList("NewRoleMapper.listAllRolesHasSuper", null);
    }

    /**
     * 添加角色
     * 
     * @param role
     * @throws Exception
     */
    public void addRole(Role role) throws Exception {
        dao.save("NewRoleMapper.insertRole", role);
    }

    /**
     * 通过id查找
     * 
     * @param pageData
     * @return
     * @throws Exception
     */
    public PageData findRoleById(PageData pageData) throws Exception {
        return (PageData) dao.findForObject("NewRoleMapper.findRoleById", pageData);
    }

    /**
     * 编辑角色
     * 
     * @param pageData
     * @return
     * @throws Exception
     */
    public PageData editRole(PageData pageData) throws Exception {
        return (PageData) dao.findForObject("NewRoleMapper.editRole", pageData);
    }

    /**
     * 列出此角色下的所有用户
     * 
     * @param pageData
     * @return
     * @throws Exception
     */
    public List<PageData> listAllUserByRoleId(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("NewRoleMapper.listAllUserByRoleId", pageData);
    }

    /**
     * 删除角色
     * 
     * @param roleid
     * @throws Exception
     */
    public void deleteRoleById(Integer roleid) throws Exception {
        dao.delete("NewRoleMapper.deleteRoleById", roleid);
    }

    /**
     * 取最大id
     * findMaxId(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pd
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：PageData
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public PageData findMaxId(PageData pageData) throws Exception {
        return (PageData) dao.findForObject("NewRoleMapper.findMaxRoleId", pageData);
    }

    /**
     * 
     * getRoleById(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param roleid
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：Role
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public Role getRoleById(Integer roleid) throws Exception {
        return (Role) dao.findForObject("NewRoleMapper.getRoleById", roleid);
    }

    /**
     * 
     * queryAllModuleRelaByRoleId(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param roleid
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：List<PageData>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public List<PageData> queryAllModuleRelaByRoleId(Integer roleid) throws Exception {
        return (List<PageData>) dao.findForList("NewRoleMapper.queryAllModuleRelaByRoleId", roleid);
    }

    /**
     * 
     * @param roleid
     * @return
     * @throws Exception
     */
    public List<Module> queryAllModulesByRoleId(Integer roleid) throws Exception {
        // 得到所有角色已授权模块
        List<Module> roleModuleList = (List<Module>) dao.findForList("NewRoleMapper.queryAllModulesByRoleId", roleid);

        PageData pageData;
        // 循环已授权的模块，找到所有已授权的操作权限
        for (Module module : roleModuleList) {
            pageData = new PageData();
            pageData.put("roleid", roleid);
            pageData.put("moduleid", module.getModuleid());
            List<OperPrivilege> operPrivilegeList = (List<OperPrivilege>) dao.findForList("NewRoleMapper.queryAllPrivilegesByRoleId", pageData);
            module.setOperprivileges(operPrivilegeList);
        }
        return roleModuleList;
    }

    /**
     * 
     * @param roleid
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<Module> queryAllModulesByRoleIds(Number[] roleids) throws Exception {
        // 得到所有角色已授权模块
        List<Module> roleModuleList = (List<Module>) dao.findForList("NewRoleMapper.queryAllModulesByRoleIds", roleids);

        PageData pageData;
        // 循环已授权的模块，找到所有已授权的操作权限
        for (Module module : roleModuleList) {
            pageData = new PageData();
            pageData.put("roleid", roleids);
            pageData.put("moduleid", module.getModuleid());
            List<OperPrivilege> operPrivilegeList = (List<OperPrivilege>) dao.findForList("NewRoleMapper.queryAllPrivilegesByRoleIds", pageData);
            module.setOperprivileges(operPrivilegeList);
        }
        return roleModuleList;
    }

    /**
     * 
     * deletePrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param roleid
     * 标记：@param authedModuleList
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void deletePrivilege(Integer roleid, List<Module> authedModuleList) {
        // TODO Auto-generated method stub
        for (Module module : authedModuleList) {
            try {
                deleteRoleModuleByRoleId(roleid, module.getModuleid());
                for (Module subModule : module.getSubmodule()) {
                    deleteRoleModuleByRoleId(roleid, module.getModuleid());
                    for (OperPrivilege operPrivilege : subModule.getOperprivileges()) {
                        deleteRoleOperPrivilegeByRoleId(roleid, module.getModuleid(), operPrivilege.getOperprivilegeid());
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 
     * deleteRoleModuleByRoleId(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param roleid
     * 标记：@param moduleid
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void deleteRoleModuleByRoleId(Integer roleid, Integer moduleid) throws Exception {
        PageData pageData = new PageData();
        pageData.put("roleid", roleid);
        pageData.put("moduleid", moduleid);
        dao.delete("NewRoleMapper.deleteRoleModuleByRoleId", pageData);
    }

    /**
     * 
     * deleteRoleOperPrivilegeByRoleId(
     * 这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param roleid
     * 标记：@param moduleid
     * 标记：@param operid
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void deleteRoleOperPrivilegeByRoleId(Integer roleid, Integer moduleid, Integer operid) throws Exception {
        PageData pageData = new PageData();
        pageData.put("roleid", roleid);
        pageData.put("moduleid", moduleid);
        pageData.put("operprivilegeid", operid);
        dao.delete("NewRoleMapper.deleteRoleOperPrivilegeByRoleId", pageData);
    }

    /**
     * 
     * addRoleModule(添加角色与模块的绑定关系)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void addRoleModule(PageData pageData) throws Exception {
        pageData.put("roleid", Integer.valueOf(pageData.get("roleid").toString()));
        pageData.put("moduleid", Integer.valueOf(pageData.get("moduleid").toString()));
        dao.findForList("NewRoleMapper.addRoleModule", pageData);
    }

    /**
     * 
     * addRoleOperPrivilege(添加角色与操作权限的绑定关系)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void addRoleOperPrivilege(PageData pageData) throws Exception {
        pageData.put("roleid", Integer.valueOf(pageData.get("roleid").toString()));
        pageData.put("moduleid", Integer.valueOf(pageData.get("moduleid").toString()));
        pageData.put("operprivilegeid", Integer.valueOf(pageData.get("operprivilegeid").toString()));
        dao.findForList("NewRoleMapper.addRoleOperPrivilege", pageData);
    }

    /**
     * saveAuthPrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param roleid
     * 标记：@param filterResultDelete
     * 标记：@param filterResultCreate
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @throws Exception
     * 
     * @exception
     * @since 1.0.0
     */
    public void saveAuthPrivilege(Integer roleid, List<String> filterResultDelete, List<String> filterResultCreate) throws Exception {
        // TODO Auto-generated method stub
        // 需删除的权限ID绑定关系
        for (String privilegeDelete : filterResultDelete) {
            // 操作权限ID
            if (isContain(privilegeDelete, "-")) {
                String[] privilegeDeletes = privilegeDelete.split("-");
                deleteRoleOperPrivilegeByRoleId(roleid, Integer.valueOf(privilegeDeletes[0]), Integer.valueOf(privilegeDeletes[1]));
            } else {
                // 功能权限ID
                deleteRoleModuleByRoleId(roleid, Integer.valueOf(privilegeDelete));
            }
        }

        // 需新增的权限ID绑定关系
        for (String privilegeCreate : filterResultCreate) {
            // 操作权限ID
            if (isContain(privilegeCreate, "-")) {
                String[] privilegeCreates = privilegeCreate.split("-");
                PageData pageDataOper = new PageData();
                pageDataOper.put("roleid", roleid);
                pageDataOper.put("moduleid", privilegeCreates[0]);
                pageDataOper.put("operprivilegeid", privilegeCreates[1]);
                addRoleOperPrivilege(pageDataOper);
            } else {
                // 功能权限ID
                PageData pageDataModule = new PageData();
                pageDataModule.put("roleid", roleid);
                pageDataModule.put("moduleid", privilegeCreate);
                addRoleModule(pageDataModule);
            }
        }
    }

    /**
     * 
     * isContain(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param str1
     * 标记：@param str2
     * 标记：@return
     * 返回值：boolean
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public static boolean isContain(String str1, String str2) {
        return str1.contains(str2);
    }

}
